// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db=cloud.database()
const _=db.command
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let openid = wxContext.OPENID;
  let userData=event.userData;

  let proc=await db.collection('friend').where({
    openid
  }).count();

  if(proc.total==0){
    //新用户加入freind表
    await db.collection('friend').add({
      data:{
        openid,
        userInfo:userData.data,
        friends:[]
      }
    })
  }

  else{
    //老用户更新信息
    await db.collection('friend').where({
      openid
    }).update({
      data:{
        userInfo: userData.data
      }
    })
  }
}